package com.nuoyuan.wx.wx.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuoyuan.wx.aop.NoLoginRequired;
import com.nuoyuan.wx.common.constant.PublishTypeEnum;
import com.nuoyuan.wx.common.controller.BaseController;
import com.nuoyuan.wx.common.dto.BaseRespDto;
import com.nuoyuan.wx.common.utils.GsonUtil;
import com.nuoyuan.wx.dao.po.ProductPo;
import com.nuoyuan.wx.service.ProductService;
import com.nuoyuan.wx.vo.DownloadFilleVo;
import com.nuoyuan.wx.vo.PageVo;
import com.nuoyuan.wx.vo.ProductQueryVo;
import com.nuoyuan.wx.vo.ProductVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

/**
 * @Package com.nuoyuan.wx.wx.controller
 * @ClassName ProductController
 * @Description TODO
 * @Author linus
 * @Date 2020-02-21 16:06
 * @Version 1.0
 **/
@RestController
public class ProductController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProductService productService;
    /**
     * 列表
     * @param query
     * @param request
     * @param response
     * @return
     */
    @NoLoginRequired
    @RequestMapping(value = "/wx/products", method = RequestMethod.GET)
    @ResponseBody
    public String list(ProductQueryVo query, HttpServletRequest request, HttpServletResponse response) {
        Wrapper<ProductPo> queryWrapper = new QueryWrapper<>();
        boolean isSale = query.getIfEnd() ? false : true;
        ((QueryWrapper<ProductPo>) queryWrapper).eq("publish_type", PublishTypeEnum.valueOf(query.getPublishType()).name()).eq("is_sale", isSale).orderByDesc("product_id");
        BaseRespDto<PageVo<ProductVo>> resp = productService.getList(query, queryWrapper);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }
        return GsonUtil.toJson(resp.getData());
    }

    /**
     * 详情
     * @param param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/wx/product", method = RequestMethod.GET)
    @ResponseBody
    public String detail(ProductVo param, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto<ProductVo> resp = productService.getDetail(param);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }

        return GsonUtil.toJson(resp.getData());
    }


    /**
     * 文件下载
     * @param param
     * @param response
     * @throws IOException
     */
    @RequestMapping("/wx/product/download")
    @ResponseBody
    public void download(@RequestBody DownloadFilleVo param, HttpServletResponse response) throws IOException {
        //通知浏览器以附件形式下载
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(param.getObjectName().getBytes(), "ISO-8859-1"));

        // 读取文件内容。
        BufferedInputStream in = new BufferedInputStream(productService.getDownloadFile(param.getObjectName()).getObjectContent());
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        byte[] buffer = new byte[1024];
        int lenght = 0;
        while ((lenght = in.read(buffer)) != -1) {
            out.write(buffer, 0, lenght);
        }
        if (out != null) {
            out.flush();
            out.close();
        }
        if (in != null) {
            in.close();
        }
    }
}
